package com.cms.validate;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.cms.common.kit.StrKit;
import com.cms.common.util.NumberValidationUtils;

public class ValidForm {
	
	private final static Logger logger=Logger.getLogger(ValidForm.class);
	
	
	public static String validform(HttpServletRequest request,List<Rule> rules){
		if(rules.size()==0)return "";
		String errorMsg="";
		Map<String, String[]> parameterMap = request.getParameterMap();
		for (Rule rule : rules) {
			//验证 参数是否在parameterMap里面
			if(parameterMap.keySet().contains(rule.getField())){
				errorMsg=execute(rule,parameterMap.get(rule.getField()));
			}else{
				errorMsg=rule.getField()+"不可以为空！";
			}
		}
		return errorMsg;
	}

	private static String execute(Rule rule, String[] strings) {
		if(StrKit.isBlank(rule.getRules()))return "";
		String errorMsg="";
		String parameter=strings[0].trim();
		//规则列表
		String[] rules=rule.getRules().split("|");
		for (String str_rule : rules) {
			//TODO 需要改成switch
			if (StrKit.isEqual(str_rule,"required")&&StrKit.isBlank(parameter)){errorMsg=rule.getLabel()+"不可以为空！";break;}
			if (StrKit.isEqual(str_rule,"email")&&!StrKit.isEmail(parameter)){errorMsg=rule.getLabel()+"不合乎Email规范！";break;}
			if (StrKit.isEqual(str_rule,"integer")&&!NumberValidationUtils.isWholeNumber(parameter)){errorMsg=rule.getLabel()+"不是整数！";break;}
			if (StrKit.isEqual(str_rule,"integer")&&StringUtils.isNumeric(parameter)){errorMsg=rule.getLabel()+"不是整数！";break;}
			if (StringUtils.substring(str_rule, 0, 9).equalsIgnoreCase("callback_"))
			{
				str_rule = str_rule.substring(0, 9);//完整类名->方法名
				errorMsg=valid_function(rule,str_rule,parameter);
			}
			
		}
		return errorMsg;
	}
	
	private static String valid_function(Rule rule, String str_rule, String parameter) {
		//该类是否存在方法，
		String errorMsg="";
		String[] _temp=str_rule.split("->");
		String wholeClassName=_temp[0];
		String methodName=_temp[1];
		Class<?> clazz;
		try {
			clazz = Class.forName(wholeClassName);
			if(clazz != null){
				Object newInstance = clazz.newInstance();
				Method method = clazz.getMethod(methodName, String.class);
				method.setAccessible(true);
				boolean result = (Boolean)method.invoke(newInstance, parameter);
				if(!result)errorMsg=rule.getLabel()+"验证失败!";
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			logger.error("没有找到验证类！"+wholeClassName);
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
			logger.error("没有找到验证类的方法!"+wholeClassName+"->"+methodName);
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return errorMsg;
	}

	public static void main(String[] args) {
		System.out.println(StringUtils.isNumeric("-1"));
	}
	
	
}
